﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BEELab
{
    public partial class EditDataElement : System.Web.UI.Page
    {
        /// <summary>
        /// Occures on every page load (first time or Post Back).
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["ID"] != null && BEELab.Logic.ProgramManagement.StageManagement.DataElements.GetOwnerID(int.Parse(Request.QueryString["ID"].ToString())) != int.Parse(Session["UserID"].ToString()) && (Session["Admin"] == null || (bool)Session["Admin"] == false))
            {
                Response.Redirect("Main.aspx");
            }

            if (!IsPostBack && Request.QueryString["ID"] != null)
            {
                BEELab.Logic.ProgramManagement.StageManagement.DataElements.DataElement element = new Logic.ProgramManagement.StageManagement.DataElements.DataElement(int.Parse(Request.QueryString["ID"].ToString()));

                string labels = BEELab.Logic.ProgramManagement.StageManagement.DataElements.EnumerateDataElementsForEdit(element.StageID, element.Label, "|");
                if (labels.Length > 0)
                {
                    RegexDataLabel.ValidationExpression = "^(?!" + labels + ").*$";
                }
                else
                {
                    RegexDataLabel.ValidationExpression = ".*";
                }

                txtLabel.Text = element.Label;
                ddlVariable.SelectedValue = element.VariableID.ToString();
            }
            else
            {
                literal1.Text = "<script>parent.$.fancybox.close(); window.parent.location.reload();    </script>";
            }
        }

        /// <summary>
        /// Updates Data Element details (Label and VariableID) on DB 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button3_Click(object sender, EventArgs e)
        {
            BEELab.Logic.ProgramManagement.StageManagement.DataElements.UpdateElement(int.Parse(Request.QueryString["ID"].ToString()), txtLabel.Text, int.Parse(ddlVariable.SelectedValue));
        }
    }
}